package IDE;


import java.awt.List;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author angelzambrano
 */
public class ZTexArea implements KeyListener {

    private JTextArea area;
    private int MASK;
    private String palabras[];
    private JDialog dialog;

    public ZTexArea(JTextArea area, JFrame fra) {
        this.area = area;
        area.append("//Compilador..\n");
        area.addKeyListener(this);
        MASK = 0;
        /***/
        palabras = new String[33];
        palabras[0] = "Entero";
        palabras[1] = "Simple";
        palabras[2] = "Doble";
        palabras[3] = "Caracter";
        palabras[4] = "Cadena";
        palabras[5] = "Logico";

        palabras[6] = "VAR";
        palabras[7] = "CONST";
        palabras[8] = "ALGORITMO";
        palabras[9] = "INICIO";

        palabras[10] = "Y";
        palabras[11] = "O";
        palabras[12] = "NO";
        palabras[13] = "INICIO";

        palabras[13] = "RAIZ(";

        palabras[14] = "LOG(";
        palabras[15] = "LN(";
        palabras[16] = "ABS(";
        palabras[17] = "REDONDEA(";
        palabras[18] = "TRUNCA(";
        palabras[19] = "EXP(";
        palabras[20] = "SEN(";
        palabras[21] = "ASEN(";
        palabras[22] = "COS(";
        palabras[23] = "ACOS(";
        palabras[24] = "TAN(";
        palabras[25] = "TAN(";
        palabras[26] = "LEE ";
        palabras[27] = "MUESTRE";
        palabras[28] = "FIN";
        palabras[29] = "SI";
        palabras[30] = "ENTONCES";
        palabras[31] = "CASO";
        palabras[32] = "PARA";
        dialog = new JDialog(fra, "lista", true);
        dialog = new JDialog();
        dialog.setBounds(400, 200, 300, 300);

    }

    public void keyTyped(KeyEvent e) {

    }


    public void keyPressed(KeyEvent e) {
        if (MASK == 0) {
            if (e.getKeyCode() == 17) {
                MASK = 17;
            }
        }
        if (MASK == 17) {
            if (e.getKeyCode() == 32) {
                if (area.getCaretPosition() > 0) {
                    try {
                        String verificar = area.getText(area.getCaretPosition() - 1, 1);
                        if (verificar.compareTo(" ") != 0) {
                            int i = 1;
                            verificar = "";
                            while ((!area.getText(area.getCaretPosition() - i, i).contains(" ") && !area.getText(area.getCaretPosition() - i, i).contains("\n")) && area.getCaretPosition() - i > 0) {
                                verificar = area.getText(area.getCaretPosition() - i, i);
                                i++;
                            }
                            //dialog.setLayout(null);
                            dialog.getContentPane().removeAll();
                            final List lista = new List();
                            for (int j = 0; j < palabras.length; j++) {
                                int sh = 1;
                                int k = 0;
                                while (k < verificar.length() && k < palabras[j].length() && sh == 1) {
                                    if (!(verificar.charAt(k) == palabras[j].charAt(k))) {
                                        sh = 0;
                                    }
                                    k++;
                                }
                                if (sh == 1) {
                                    lista.addItem(palabras[j]);
                                }
                            }
                            lista.setBounds(30, 30, 230, 230);
                            final int len = verificar.length();
                            lista.addKeyListener(new KeyAdapter() {

                                @Override
                                public void keyPressed(KeyEvent even) {
                                    if (even.getKeyCode() == KeyEvent.VK_ENTER && lista.getSelectedIndex() != -1) {
                                        String sub = lista.getItem(lista.getSelectedIndex()).substring(len);
                                        area.insert(sub, area.getCaretPosition());

                                    }
                                    if (even.getKeyCode() == KeyEvent.VK_ENTER) {
                                        dialog.setVisible(false);
                                        System.gc();
                                    }
                                }
                            });
                            dialog.getContentPane().add(lista);
                            dialog.validate();
                            dialog.setVisible(true);
                        }
                    } catch (BadLocationException ex) {
                        Logger.getLogger(ZTexArea.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    public void keyReleased(KeyEvent e) {
        MASK = 0;
    }
}
